<?php
namespace App\Imports;
use PHPExcel_IOFactory;
use PHPExcel_Cell;


class ImportSign
{
    public function import($file,$cid){

        try {
            $inputFileName = './upload/excel.xls';
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch (Exception $e) {
            die('加载文件发生错误："'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }
        $sheet = $objPHPExcel->getSheet(0);
        $data = $sheet->toArray(); //该方法读取不到图片，图片需单独处理
        $imageFilePath = './upload/imgs/'; //图片本地存储的路径
        if (!file_exists($imageFilePath)) {
            mkdir($imageFilePath, 0777, true);
        }

//处理图片
        foreach ($sheet->getDrawingCollection() as $img) {
            list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列
            $imageFileName = $img->getCoordinates() . mt_rand(1000, 9999);

            switch($img->getExtension()) {
                case 'jpg':
                case 'jpeg':
                    $imageFileName .= '.jpeg';
                    $source = imagecreatefromjpeg($img->getPath());
                    imagejpeg($source, $imageFilePath.$imageFileName);
                    break;
                case 'gif':
                    $imageFileName .= '.gif';
                    $source = imagecreatefromgif($img->getPath());
                    imagejpeg($source, $imageFilePath.$imageFileName);
                    break;
                case 'png':
                    $imageFileName .= '.png';
                    $source = imagecreatefrompng($img->getPath());
                    imagejpeg($source, $imageFilePath.$imageFileName);
                    break;
            }
            $startColumn = self::ABC2decimal($startColumn);
            $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;

        }
        var_dump($data);
    }

    public function ABC2decimal($abc)
    {
        $ten = 0;
        $len = strlen($abc);
        for($i=1;$i<=$len;$i++){
            $char = substr($abc,0-$i,1);//反向获取单个字符

            $int = ord($char);
            $ten += ($int-65)*pow(26,$i-1);
        }
        return $ten;
    }
}
